import tkinter as tk
import time
import winsound

#这里设置参数
#开始时间(时，分，秒)
time_start=[1,30,15]
#文字格式（字体，文字大小）
text_font = ( "楷体", 150 )
#文字颜色
text_color = "black"
#背景颜色
color_bg = "yellow"

#开始运行程序
#键盘事件
def event_key(event):
    global count_state,win_main
    #print('key: ' + event.keysym+" event:"+str(event) )
    if event.keysym=='Escape':      #ESC退出全屏
       win_main.attributes("-fullscreen", False)
    elif event.keysym=='F11':       #F11切换全屏
       win_main.attributes("-fullscreen", True)
    elif event.keysym=='Left':         #方向键向左暂停计时
        if count_state == 2:
            count_state = 3
    elif event.keysym=='Right':         #方向键向右开始计时
        if count_state != 2:
            count_state = 1
    elif event.keysym=='Up':          #方向键向上重置时间
        count_state = 0
    win_main.mainloop()
#更新时间
def time_update():
    global time_start,count_state,count_length,count_gap,count_start,text_time,win_main
    if count_state == 0:
        count_length = int(time_start[0])*3600+int(time_start[1])*60+int(time_start[2])
        count_gap = count_length
    elif count_state == 1:
        count_start = time.time()
        count_state = 2
    elif count_state == 2:
        count_gap = max( 0, count_length-int(time.time()-count_start) )
        if count_gap <= 0:
            winsound.Beep( 2000, 1000 )
            count_state = 4
    elif count_state == 3:
        count_length = count_gap
        count_state = 4
    count_now = str(int(((count_gap)%86400)/3600))+":"
    count_now = count_now + str(int(((count_gap)%3600)/60))+":"
    count_now = count_now + str(int((count_gap)%60))
    text_time["text"] = count_now
    win_main.after(100, time_update)
#时间初始化
count_state = 0
#设置窗口
win_main = tk.Tk()
win_main.title("倒计时")
win_main.configure(bg=color_bg)
win_main.geometry("%dx%d" %(win_main.winfo_screenwidth()/2, win_main.winfo_screenheight()/2))
win_main.bind('<Key>', event_key)
text_time = tk.Label(win_main, text="TIME SHOW", font=text_font, fg=text_color, bg=color_bg )
text_time.pack( anchor="center", expand=True )
win_main.after(100, time_update)
win_main.mainloop()


